package com.theaf.core.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 日志工具类
 *
 * @author luojuan
 * @create 2017-11-27 15:24
 **/
public class LogUtils {

    private final Log log;

    private LogUtils(Class<?> clazz) {
        log = LogFactory.getLog(clazz);
    }

    private LogUtils() {
        log = LogFactory.getLog(LogUtils.class);
    }

    public static LogUtils getLog(Class<?> clazz) {
        return new LogUtils(clazz);
    }

    public  void info(Object message){
        log.info(message);
    }
    public void debug(Object message){
        log.debug(message);
    }

    public  void info(Object message,Throwable t){
        log.info(message,t);
    }

    public  void error(Object message){
        log.error(message);
    }
    public void fatal(Object message){
        log.fatal(message);
    }

    public  void error(Object message,Throwable t){
        log.error(message, t);
    }

    public  void trace(Object message){
        log.trace(message);
    }
    public String traceMsg(String msg){
        return msg;
    }

    public  void trace(Object message,Throwable t){
        log.trace(message,t);
    }

    public void logStackTrace(String exceptionFlag){
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StringBuilder stringBuilder = new StringBuilder(exceptionFlag);
        for(StackTraceElement se : stackTraceElements){
            stringBuilder.append(se.toString());
            stringBuilder.append("\n");
        }
        info(stringBuilder);
    }
    public static String processLogger(Throwable throwable){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }




}
